home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / examples / demo / demosrc / d_forecast.pro < prev    next >
Text File  |  1997-07-08  |  26KB  |  789 lines

  1. ; $Id: d_forecast.pro,v 1.18 1997/04/21 18:51:35 tremblay Exp $
  2. ;
  3. ;  Copyright (c) 1997, Research Systems, Inc. All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5. ;
  6. ;+
  7. ;  FILE:
  8. ;       d_forecast.pro
  9. ;
  10. ;  CALLING SEQUENCE: d_forecast
  11. ;
  12. ;  PURPOSE:
  13. ;       Shows forecast of a data set.
  14. ;
  15. ;  MAJOR TOPICS: Statistics and widget.
  16. ;
  17. ;  CATEGORY:
  18. ;       IDL 5.0
  19. ;
  20. ;  INTERNAL FUNCTIONS and PROCEDURES:
  21. ;       pro AutoCorr            - Compute the autocorrelation function
  22. ;       pro AutoFCast           - compute the autoforecast function
  23. ;       fun Tsdata              - Create a time series.
  24. ;       pro ForecastEventHndl   - Event handler
  25. ;       pro CleanUpForecast     - Cleanup
  26. ;       pro D_Forecast              - Main procedure
  27. ;
  28. ;  EXTERNAL FUNCTIONS, PROCEDURES, and FILES:
  29. ;       fun gettips             - Read the tip file 
  30. ;       pro widtips             - Create the tip widgets
  31. ;       pro sizetips            - Size the tip widgets
  32. ;       forecast.txt
  33. ;       forecast.tip
  34. ;
  35. ;  REFERENCE: The Analysis of Time Series, An Introduction (Fourth Edition)
  36. ;             Chapman and Hall
  37. ;             ISBN 0-412-31820-2
  38. ;
  39. ;  NAMED STRUCTURES:
  40. ;       none.
  41. ;
  42. ;  COMMON BLOCS:
  43. ;       none.
  44. ;
  45. ;  MODIFICATION HISTORY: Written by:  GGS, RSI, January 1995
  46. ;                        Modified by DAT,RSI,  July 1996 New GUI
  47. ;                        Remove common blocs.
  48. ;-
  49. ;--------------------------------------------------------------------
  50. ;
  51. ;   PURPOSE  Compute and plot the autocorrelation function.
  52. ;
  53. pro AUTOCORR, $
  54.     event        ; IN: event structure
  55.  
  56.     ;  Get the info structure.
  57.     ;
  58.     WIDGET_CONTROL, event.top, GET_UVALUE=info, /NO_COPY
  59.  
  60.     ;  This call-back function computes the sample autocorrelation function
  61.     ;  and the 95% confidence interval of cYdata. The results are plotted in
  62.     ;  the draw window, cPwindow2.
  63.  
  64.     currentYMargin = !Y.MARGIN
  65.     !Y.Margin = [3, currentYMargin[1]]
  66.  
  67.     ;  Set the active plot window to cPwindow2.
  68.     ; 
  69.     WSET, info.cPwindow2
  70.  
  71.     ;  Compute the sample autocorrelation function of cYdata for the time 
  72.     ;  indexes, [0, 1, 2, ... , cNy-2].
  73.     ;
  74.     autocorrY = A_CORRELATE(info.cYdata, INDGEN(info.cNy-1))
  75.  
  76.     ;  Compute the 95% confidence interval for cYdata.
  77.     ;
  78.     conf = 1.96 / SQRT(info.cNy)
  79.  
  80.     ;  Establish the plotting coordinate system.
  81.     ;
  82.     PLOT, autocorrY, YSTYLE=1, YMINOR=1, YTICKLEN=-0.008, $
  83.         YTICKS=1, $
  84.         YRANGE = [-1.0, 1.0], $
  85.         LINESTYLE=0, COLOR=0, BACKGROUND=1, $
  86.         YTITLE='!7r!x', XTITLE='Time Lag', $
  87.         TITLE="Time-Series Sample Autocorrelation", /NODATA
  88.  
  89.     ;  Overplot the sample autocorrelation function of cYdata.
  90.     ;
  91.     OPLOT, autocorrY, COLOR=4
  92.  
  93.     ;  Plot the upper confidence interval limit line.
  94.     ;
  95.     PLOTS, 0, conf, COLOR=0
  96.     PLOTS, info.cXmax, conf, COLOR=0, LINESTYLE=5, /CONTINUE
  97.  
  98.     ;  Plot the lower confidence interval limit line.
  99.     ;
  100.     PLOTS, 0, -conf, COLOR=0
  101.     PLOTS, info.cXmax, -conf, COLOR=0, LINESTYLE=5, /CONTINUE
  102.  
  103.     ;  Restore the information structure.
  104.     ;
  105.     WIDGET_CONTROL, event.top, SET_UVALUE=info, /NO_COPY
  106.  
  107.     !Y.Margin = currentYMargin
  108.  
  109. end  ; of AUTOCORR
  110.  
  111. ;--------------------------------------------------------------------
  112. ;
  113. ;   PURPOSE  Compute the auto forecast function.
  114. ;
  115. pro AUTOFCAST, $
  116.     event         ; IN: event structure
  117.  
  118.     ;  Get the info structure.
  119.     ;
  120.     WIDGET_CONTROL, event.top, GET_UVALUE = info, /NO_COPY
  121.  
  122.     ;  Make sure that the margin are set right.
  123.     ;
  124.     !Y.MARGIN= [6.0, 2.0]
  125.     !X.MARGIN= [10.0, 3.0]
  126.  
  127.     ;  This call-back function computes the
  128.     ;  autoregressive forecasting model 
  129.     ;  of the time-series data, cYdata.
  130.     ;  The time-series data and forecasts 
  131.     ;  are plotted in the draw window, cPwindow1.
  132.  
  133.   
  134.     ;  Compute (cNfcast) forecasts using a model of order (cOrder).
  135.     ;
  136.     info.cFuture = TS_FCAST(info.cYdata, info.cOrder, info.cNfcast) 
  137.  
  138.     ;  Set the active plot window to cPwindow1.
  139.     ;
  140.     WSET, info.cPwindow1
  141.  
  142.     ;  Establish the plotting coordinate system.
  143.     ;
  144.     PLOT, info.cXdata, info.cYdata, $
  145.         YTITLE='Y', XTITLE='Time', $
  146.         XRANGE=[0, info.cXmax+info.cFcastmax], XSTYLE=2,$ 
  147.         YRANGE=[info.cYmin-1, info.cYmax+1], YSTYLE=2, $
  148.         YMINOR=1, YTICKLEN=-0.008, LINESTYLE=0,  $
  149.         CHARSIZE=1.0*info.charscale, COLOR=0, BACKGROUND=1, /NODATA
  150.  
  151.     ;  Overplot the time-series data with a "diamond" symbol.
  152.     ;
  153.     OPLOT, info.cXdata, info.cYdata, PSYM=4, SYMSIZE=2, COLOR=4
  154.  
  155.     ;  Overplot the forecast data with a "triangle" symbol.
  156.     ;
  157.     OPLOT, [info.cXmax+indgen(info.cNfcast)+1], info.cFuture, $
  158.         PSYM=5, SYMSIZE=1.5, COLOR=2
  159.  
  160.     ;  Compute a smooth spline fit to the
  161.     ;  time-series and forecast data by
  162.     ;  increasing the number of points by a factor(fac).
  163.     ;
  164.     fac = 10
  165.     xint = FINDGEN(fac*MAX(info.cXdata+info.cNfcast)+1) / fac
  166.     yint = SPLINE(FINDGEN(info.cNy+info.cNfcast), $
  167.         [info.cYdata,info.cFuture], xint, 0.1)
  168.  
  169.     ;  Overplot the time-series and forecast data with the spline fit.
  170.     ;     
  171.     OPLOT, xint, yint, LINESTYLE=0, COLOR=0
  172.  
  173.     ;  Create a symbol legend using the NORMAL coordinate system.
  174.     ;  Plot a "diamond" symbol.  
  175.     ;  Set Y position 1/3 of the way between the lower edge of the 
  176.     ;  window and the plot window.
  177.     ;
  178.     ypos = !Y.WINDOW[0] / 3.0
  179.     PLOTS, 0.18, ypos+0.01, PSYM=4, SYMSIZE=2, COLOR=4, /NORMAL
  180.  
  181.     ;  Plot the corresponding label.
  182.     ; 
  183.     XYOUTS, 0.20, ypos, "Time-Series Data", COLOR=0, /NORMAL
  184.  
  185.     ;  Plot a "triangle" symbol.
  186.     ;
  187.     PLOTS, 0.58, ypos+0.015, PSYM=5, SYMSIZE=1.5, COLOR=2, /NORMAL
  188.  
  189.     ;  Plot the corresponding label.
  190.     ;
  191.     XYOUTS, 0.60, ypos, "Autoregressive Forecasts", COLOR = 0, /NORMAL
  192.  
  193.     ;  Restore the info structure.
  194.     ;
  195.     WIDGET_CONTROL, event.top, SET_UVALUE = info, /NO_COPY
  196.  
  197. end  ; of AUTOFCAST
  198.  
  199. ;--------------------------------------------------------------------
  200. ;
  201. ;   PURPOSE  Generate a time series data.
  202. ;
  203. function TSDATA, $
  204.     RANDOM = random    ; IN: (opt) Generate a random time series.
  205.  
  206.     ;  This call-back function defines either
  207.     ;  a pseudo-random or static time-series.
  208.  
  209.     ;  Create a pseudo-random time-series.
  210.     ;
  211.     if (KEYWORD_SET(random) NE 0) then begin
  212.        cYdata = SMOOTH(RANDOMN(seed, 41) * RANDOMU(seed, 41) * 50, 5) 
  213.     endif else begin
  214.  
  215.        ; Uniformly sampled time-series data.
  216.        ;
  217.        cYdata = $
  218.            [66.85, 67.08, 70.79, 69.90, 66.76, 67.43, 65.92, 66.31, $
  219.             67.12, 67.18, 65.05, 66.40, 65.54, 66.08, 65.39, 66.51, $
  220.             67.25, 69.12, 68.45, 68.57, 69.57, 67.05, 67.41, 65.81, $
  221.             67.70, 67.92, 69.67, 68.20, 67.72, 67.56, 65.30, 65.46, $
  222.             63.41, 64.98, 66.03, 66.80, 66.79, 65.96, 67.47, 69.02, $
  223.                                                              69.88]
  224.     endelse
  225.  
  226.     cNy = N_ELEMENTS(cYdata)  ; Number of time-series points.
  227.     cXdata = LINDGEN(cNy)     ; Indexed time intervals, [0, 1, 2, ... , Cny-1].
  228.     cXmax = MAX(cXdata)       ; Maximum x value.
  229.     cYmax = MAX(cYdata)       ; Maximum y value.
  230.     cYmin = MIN(cYdata)       ; Minimum y value.
  231.     cFcastmax = cNy - 1       ; Maximum number of forecasts.
  232.     
  233.     ;  Define the inside structure for this function.
  234.     ;
  235.     insideStr={cYdata:cYdata, $
  236.         cNy:cNy, $
  237.         cXdata:cXdata,$
  238.         cXmax:cXmax,$
  239.         cYmax:cYmax,$
  240.         cYmin:cYmin,$
  241.         cFcastmax:cFcastmax }
  242.               
  243.     ;  Return the insideStr which contains the data information.
  244.     ;
  245.     RETURN, insideStr
  246.    
  247. end   ; of TSDATA
  248.  
  249. ;--------------------------------------------------------------------
  250. ;
  251. ;   PURPOSE  Handle the forecasting events.
  252. ;
  253. pro ForecastEventHdlr, event
  254.  
  255.     ;  Handle the 'close' or 'Quit' event for the
  256.     ;  close box.
  257.     ;
  258.     if (TAG_NAMES(event, /STRUCTURE_NAME) EQ $
  259.         'WIDGET_KILL_REQUEST') then begin
  260.         WIDGET_CONTROL, event.top, /DESTROY
  261.         RETURN
  262.     endif
  263.  
  264.     ;  This procedure is the event handler.
  265.     ;  Get the info structure from top-level base.
  266.     ;
  267.     WIDGET_CONTROL, event.top, GET_UVALUE=info, /NO_COPY
  268.    
  269.     ;  Determine which event.
  270.     ; 
  271.     WIDGET_CONTROL, event.id, GET_UVALUE=eventval
  272.  
  273.     ;  Take the following action based on the corresponding event.
  274.     ;
  275.     CASE eventval OF
  276.  
  277.         "GenerateTS" :  begin
  278.             ;  Desensitize the buttons
  279.             ;
  280.             WIDGET_CONTROL, info.wOrderSlider, SENSITIVE=0 
  281.             WIDGET_CONTROL, info.wNumberSlider, SENSITIVE=0
  282.             WIDGET_CONTROL, info.wAboutButton, SENSITIVE=0
  283.             WIDGET_CONTROL, info.wGenerateButton, SENSITIVE=0
  284.             WIDGET_CONTROL, info.wQuitButton, SENSITIVE=0
  285.        
  286.             ;  Activate the HOURGLASS cursor to indicate 
  287.             ;  a computationally intensive event.
  288.             ;
  289.             WIDGET_CONTROL, info.wGenerateButton, /HOURGLASS
  290.  
  291.             ;  Generate a pseudo-random time-series.
  292.             ;
  293.             TSdataStr=TSDATA(/random)
  294.                        
  295.             ;  Copy the TSdataStr into the info structure.
  296.             ;
  297.             info.cYdata=TSdataStr.cYdata      
  298.             info.cXdata=TSdataStr.cXdata      
  299.             info.cYmax=TSdataStr.cYmax        
  300.             info.cXmax=TSdataStr.cXmax        
  301.             info.cYmin=TSdataStr.cYmin        
  302.             info.cNy=TSdataStr.cNy            
  303.             info.cFcastmax=TSdataStr.cFcastmax
  304.  
  305.             ;  Compute the autoregressive forecasting model.
  306.             ; 
  307.             WIDGET_CONTROL, event.top, SET_UVALUE=info, /NO_COPY
  308.             AUTOFCAST, event
  309.  
  310.             ;  Compute the sample autocorrelation function.
  311.             ;
  312.             AUTOCORR, event
  313.         
  314.             ;  Get the info structure.
  315.             ;
  316.             WIDGET_CONTROL, event.top, GET_UVALUE=info, /NO_COPY
  317.         
  318.             ;  Sensitize the buttons.
  319.             ;
  320.             WIDGET_CONTROL, info.wOrderSlider, SENSITIVE=1 
  321.             WIDGET_CONTROL, info.wNumberSlider, SENSITIVE=1
  322.             WIDGET_CONTROL, info.wAboutButton, SENSITIVE=1
  323.             WIDGET_CONTROL, info.wGenerateButton, SENSITIVE=1
  324.             WIDGET_CONTROL, info.wQuitButton, SENSITIVE=1
  325.                       
  326.             ;  Restore the info structure.
  327.             ;
  328.             WIDGET_CONTROL, event.top, SET_UVALUE=info, /NO_COPY
  329.  
  330.         end    ;   of GenerateTS
  331.  
  332.         "INFO" : begin
  333.         
  334.             ; Desensitize the buttons
  335.             ;
  336.             WIDGET_CONTROL, info.wOrderSlider, SENSITIVE=0 
  337.             WIDGET_CONTROL, info.wNumberSlider, SENSITIVE=0
  338.             WIDGET_CONTROL, info.wAboutButton, SENSITIVE=0
  339.             WIDGET_CONTROL, info.wGenerateButton, SENSITIVE=0
  340.             WIDGET_CONTROL, info.wQuitButton, SENSITIVE=0
  341.         
  342.             ;  Display the information file.
  343.             ;
  344.             if (Xregistered('XDisplayFile') EQ 0) then begin
  345.                 XDisplayFile, filepath("forecast.txt", $
  346.                     SUBDIR=['examples','demo','demotext']), $
  347.                     DONE_BUTTON='Done', $
  348.                     TITLE="Time series forecasting information", $
  349.                     GROUP=event.top, WIDTH=55, HEIGHT=14
  350.             endif
  351.  
  352.             ; Sensitize the buttons.
  353.             ;
  354.             WIDGET_CONTROL, info.wOrderSlider, SENSITIVE=1 
  355.             WIDGET_CONTROL, info.wNumberSlider, SENSITIVE=1
  356.             WIDGET_CONTROL, info.wAboutButton, SENSITIVE=1
  357.             WIDGET_CONTROL, info.wGenerateButton, SENSITIVE=1
  358.             WIDGET_CONTROL, info.wQuitButton, SENSITIVE=1
  359.             
  360.             ;  Restore the info structure.
  361.             ;
  362.             WIDGET_CONTROL, event.top, SET_UVALUE=info, /NO_COPY
  363.       
  364.         end     ;   of   INFO
  365.                    
  366.          "QUIT" :    begin
  367.      
  368.             ;  Restore the info structure before destroying event.top.
  369.             ;
  370.             WIDGET_CONTROL, event.top, SET_UVALUE=info, /NO_COPY
  371.                    
  372.             ;  Destroy widget hierarchy.
  373.             ;
  374.              WIDGET_CONTROL, event.top, /DESTROY
  375.          
  376.         end     ;   of   QUIT
  377.  
  378.         "SETOrder" :   begin
  379.         
  380.             ;  Desensitize the buttons.
  381.             ;
  382.             WIDGET_CONTROL, info.wOrderSlider, SENSITIVE=1 
  383.             WIDGET_CONTROL, info.wNumberSlider, SENSITIVE=0
  384.             WIDGET_CONTROL, info.wAboutButton, SENSITIVE=0
  385.             WIDGET_CONTROL, info.wGenerateButton, SENSITIVE=0
  386.             WIDGET_CONTROL, info.wQuitButton, SENSITIVE=0
  387.         
  388.             ;  Determine the order of the forecasting model.
  389.             ;
  390.             WIDGET_CONTROL, info.wOrderSlider, GET_VALUE=cOrder
  391.                 info.cOrder=cOrder
  392.                       
  393.             ;  Compute the forecast using a model of order (cOrder).
  394.             ;
  395.             WIDGET_CONTROL, event.top, SET_UVALUE=info, /NO_COPY
  396.             AUTOFCAST, event
  397.             WIDGET_CONTROL, event.top, GET_UVALUE=info, /NO_COPY
  398.         
  399.             ;  Sensitize the buttons.
  400.             ;
  401.             WIDGET_CONTROL, info.wOrderSlider, SENSITIVE=1 
  402.             WIDGET_CONTROL, info.wNumberSlider, SENSITIVE=1
  403.             WIDGET_CONTROL, info.wAboutButton, SENSITIVE=1
  404.             WIDGET_CONTROL, info.wGenerateButton, SENSITIVE=1
  405.             WIDGET_CONTROL, info.wQuitButton, SENSITIVE=1
  406.                       
  407.             ;  Restore the info structure.
  408.             ; 
  409.             WIDGET_CONTROL, event.top, SET_UVALUE=info, /NO_COPY
  410.         
  411.         END
  412.  
  413.         "SETNumber" :  begin
  414.      
  415.             ;  Desensitize the buttons.
  416.             ;
  417.             WIDGET_CONTROL, info.wOrderSlider, SENSITIVE=0 
  418.             WIDGET_CONTROL, info.wNumberSlider, SENSITIVE=1
  419.             WIDGET_CONTROL, info.wAboutButton, SENSITIVE=0
  420.             WIDGET_CONTROL, info.wGenerateButton, SENSITIVE=0
  421.             WIDGET_CONTROL, info.wQuitButton, SENSITIVE=0
  422.         
  423.             ;  Determine the number of the forecasts.
  424.             ;
  425.             WIDGET_CONTROL, info.wNumberSlider, GET_VALUE=cNfcast
  426.                info.cNfcast=cNfcast
  427.                       
  428.             ;  Compute (cNfcast) forecasts.
  429.             ;
  430.             WIDGET_CONTROL, event.top, SET_UVALUE=info, /NO_COPY
  431.             AUTOFCAST, event
  432.             WIDGET_CONTROL, event.top, GET_UVALUE=info, /NO_COPY
  433.         
  434.             ;  Sensitize the buttons.
  435.             ;
  436.             WIDGET_CONTROL, info.wOrderSlider, SENSITIVE=1 
  437.             WIDGET_CONTROL, info.wNumberSlider, SENSITIVE=1
  438.             WIDGET_CONTROL, info.wAboutButton, SENSITIVE=1
  439.             WIDGET_CONTROL, info.wGenerateButton, SENSITIVE=1
  440.             WIDGET_CONTROL, info.wQuitButton, SENSITIVE=1
  441.                       
  442.             ;  Restore the info structure .
  443.             ;
  444.             WIDGET_CONTROL, event.top, SET_UVALUE=info, /NO_COPY
  445.         
  446.         end
  447.      
  448.     endcase
  449.    
  450. end      ;    of  ForecastEventHdlr
  451.  
  452. ;--------------------------------------------------------------------
  453. ;
  454. ;   PURPOSE  : Cleanup procedure.
  455. ;
  456. pro CleanUpForecast, wTop
  457.  
  458.     ;  Get the color table saved in the window's user value.
  459.     ;
  460.     WIDGET_CONTROL, wTop, GET_UVALUE=info, /NO_COPY
  461.    
  462.     ;  Restore the previous color table.
  463.     ;
  464.     TVLCT, info.colorTable
  465.  
  466.     ;  Restore the previous plot font.
  467.     ;
  468.     !P.FONT = info.plotFont
  469.  
  470.     ;  Restore the !X and !Y system variables.
  471.     ;
  472.     !X = info.previousX
  473.     !Y = info.previousY
  474.  
  475.     ;  Map the group leader base if it exists.
  476.     ;
  477.     if (WIDGET_INFO(info.groupBase, /VALID_ID)) then $
  478.         WIDGET_CONTROL, info.groupBase, /MAP
  479.  
  480. end   ; of CleanupForecast
  481.  
  482. ;--------------------------------------------------------------------
  483. ;
  484. ;   PURPOSE  Forecast main procedure.
  485. ;
  486. pro D_Forecast, $
  487.     GROUP=group, $   ; IN: (opt) group identifier
  488.     APPTLB = appTLB    ; OUT: (opt) TLB of this application
  489.  
  490.     ;  Check the validity of the group identifier.
  491.     ;
  492.     ngroup = N_ELEMENTS(group)
  493.     if (ngroup NE 0) then begin
  494.         check = WIDGET_INFO(group, /VALID_ID)
  495.         if (check NE 1) then begin
  496.             print,'Error, the group identifier is not valid'
  497.             print, 'Return to the main application'
  498.             RETURN
  499.         endif
  500.         groupBase = group
  501.     endif else groupBase = 0L
  502.  
  503.     ;  Make the system to have a maximum of 256 colors.
  504.     ;
  505.     numcolors = !d.N_COLORS
  506.  
  507.     if ((( !D.NAME EQ 'X') OR (!D.NAME EQ 'MAC')) $
  508.        AND (!D.N_COLORS GE 256L)) then begin
  509.        DEVICE, PSEUDO_COLOR=8
  510.     endif
  511.  
  512.     DEVICE, DECOMPOSED=0, BYPASS_TRANSLATION = 0
  513.  
  514.     ;  Get the current color table, retore it when exiting.
  515.     ;
  516.     TVLCT, savedR, savedG, savedB, /GET
  517.  
  518.     ;  Store the previuous !Y and !X settings. They
  519.     ;  will be restores when exiting this application.
  520.     ;
  521.     previousY = !Y
  522.     previousX = !X
  523.  
  524.     ;  Build color table from color vectors.
  525.     ;
  526.     colorTable = [[savedR],[savedG],[savedB]]
  527.  
  528.     ;  Save the current plot font in order to restore it when
  529.     ;  the spiro application is exited.
  530.     ;
  531.     plotFont = !P.FONT
  532.  
  533.     ;  Get the screen size.
  534.     ;
  535.     DEVICE, GET_SCREEN_SIZE = screenSize
  536.  
  537.     ;  Create the starting up message.
  538.     ;
  539.     if (ngroup EQ 0) then begin
  540.         drawbase = startmes()
  541.     endif else begin
  542.         drawbase = startmes(GROUP=group)
  543.     endelse
  544.  
  545.     ;  Call the TSdata FUNCTION to initialize the data that are
  546.     ;  contained in the structure TSdataStr.
  547.     ;
  548.     TSdataStr=TSDATA()
  549.  
  550.     ;  Define the initial forecasting parameters.
  551.     ;
  552.     cOrder = 25       ; order of the model
  553.     cNfcast = 40      ; number of forecasts
  554.   
  555.     ;  Load a predefined color tabel.
  556.     ;
  557.     LOADCT, 12, /SILENT
  558.     TEK_COLOR, 0, 8
  559.  
  560.     ;  Use harware-drawn font.
  561.     ;
  562.     !P.FONT=0
  563.  
  564.     ;  Get the tips.
  565.     ;
  566.     sText = getTips(filepath('forecast.tip', $
  567.         SUBDIR=['examples','demo', 'demotext']) )
  568.  
  569.     ;  Define a main widget base.
  570.     ;
  571.     if (N_ELEMENTS(group) EQ 0) then begin
  572.         wTopBase = WIDGET_BASE(TITLE="Time-Series Forecasting Tool",  $
  573.             /TLB_KILL_REQUEST_EVENTS, $
  574.             MAP=0, $
  575.             /COLUMN, $
  576.             TLB_FRAME_ATTR=1, Mbar=bar_base)
  577.     endif else begin
  578.         wTopBase = WIDGET_BASE(TITLE="Time-Series Forecasting Tool",  $
  579.             /TLB_KILL_REQUEST_EVENTS, $
  580.             MAP=0, $
  581.             /COLUMN, $
  582.             GROUP_LEADER=group, $
  583.             TLB_FRAME_ATTR=1, Mbar=bar_base)
  584.     endelse
  585.  
  586.         ;  Create the file|quit button.
  587.         ;
  588.         wFileButton = WIDGET_BUTTON(bar_base, VALUE='File', /MENU)
  589.  
  590.             wQuitButton = WIDGET_BUTTON(wFileButton, $
  591.                 VALUE='Quit', UVALUE='QUIT')
  592.  
  593.         ;  Create the help|about button.
  594.         ;
  595.         wHelpButton = WIDGET_BUTTON(bar_base, /HELP, VALUE='About', /MENU)
  596.  
  597.             wAboutButton = WIDGET_BUTTON(wHelpButton, $
  598.                 VALUE='About Forecasting', UVALUE='INFO')
  599.  
  600.         ;  Create the first child of the top level base(wTop).
  601.         ; 
  602.         wTopRowBase = WIDGET_BASE(wTopBase, COLUMN=2, /FRAME)
  603.  
  604.             ;  Create a base for the left column.
  605.             ;
  606.             wLeftBase = WIDGET_BASE(wTopRowBase, $
  607.                 /BASE_ALIGN_CENTER, /COLUMN)
  608.  
  609.                 ;  Put the 2 sliders
  610.                 ;  and the widget labels into a base here.
  611.                 ;
  612.                 wParamBase = WIDGET_BASE(wLeftBase, /COLUMN, /FRAME, $
  613.                    /BASE_ALIGN_CENTER, YPAD = 15)
  614.    
  615.                     wLabel1ID = WIDGET_LABEL(wParamBase, $
  616.                        VALUE="Forecasting")
  617.  
  618.                     wLabel1bID = WIDGET_LABEL(wParamBase, $
  619.                        VALUE="Parameters")
  620.  
  621.                     wLabel2ID = WIDGET_LABEL(wLeftBase, VALUE=" ")
  622.  
  623.                     ;  Define a slider widget to adjust the order
  624.                     ;  of the forecasting model.
  625.                     ;
  626.                     wOrderSlider = WIDGET_SLIDER(wParamBase, MIN=2, $
  627.                         MAX=TSdataStr.cFcastmax, VALUE=cOrder,$
  628.                         UVALUE="SETOrder", $
  629.                         TITLE="Order of the Model")
  630.  
  631.                     ;  Define a slider widget to adjust 
  632.                     ;  the number of forecasts.
  633.                     ;
  634.                     wNumberSlider = WIDGET_SLIDER(wParamBase, $
  635.                        MIN=1, MAX=TSdataStr.cFcastmax, $
  636.                        VALUE=cNfcast, $
  637.                        UVALUE="SETNumber", $
  638.                        TITLE="Number of Forecasts")
  639.  
  640.                 ;  Define a button widget to generate 
  641.                 ;  a pseudo-random time-series.
  642.                 ;
  643.                 wGenerateBase = WIDGET_BASE(wLeftBase, /COLUMN) 
  644.  
  645.                     wGenerateLabel = WIDGET_LABEL(wGenerateBase, $
  646.                         VALUE='Generate new data')
  647.  
  648.                     wGenerateButton = WIDGET_BUTTON(wGenerateBase, $
  649.                         VALUE="Generate", $
  650.                         UVALUE="GenerateTS")
  651.  
  652.             ;  Create a base for the right column.
  653.             ;
  654.             wRightBase = WIDGET_BASE(wTopRowBase, Column=1)
  655.  
  656.                ;  Define a draw widget with a horizontal dimension
  657.                ;  that is 650% of the  horizontal hardware display size and 
  658.                ;  a vertical dimension that is 30%
  659.                ;  of the horizontal hardware display size.
  660.                ;
  661.                wDraw1ID = WIDGET_DRAW(wRightBase, XSIZE=0.65*screenSize[0],  $
  662.                   YSIZE=0.34*screenSize[0], RETAIN=2)
  663.  
  664.                ;  Define a draw widget with a horizontal dimension 
  665.                ;  that is 40% of the horizontal hardware display size
  666.                ;  and a vertical dimension that is 20%
  667.                ;  of the horizontal hardware display size.
  668.                ;
  669.                wDraw2ID = WIDGET_DRAW(wRightBase, XSIZE=0.65*screenSize[0], $
  670.                    YSIZE=0.19*screenSize[0], RETAIN=2)
  671.  
  672.         ;  Create tips texts.
  673.         ;
  674.         wStatusBase = WIDGET_BASE(wTopBase, MAP=0, /ROW)
  675.  
  676.             nWidgets = 2
  677.             wText = LONARR(nWidgets)
  678.             widTips, wStatusBase, sText.text, XSIZE=36, $
  679.                 YSIZE=3, NWIDGETS=nWidgets, wText
  680.  
  681.  
  682.     ; Here,  all the widget has been created.
  683.  
  684.     ;  Realize the widget hierarchy.
  685.     ;
  686.     WIDGET_CONTROL, wTopBase, /REALIZE
  687.  
  688.     ;  Returns the top level base in the appTLB keyword.
  689.     ;
  690.     appTLB = wTopBase
  691.  
  692.     ;  Size the tips widgets.
  693.     ;
  694.     sizeTips, wTopBase, wText, wStatusBase
  695.  
  696.     ;  Setup the device to use the pointer cursors.
  697.     ;
  698.     DEVICE, /CURSOR_ORIGINAL
  699.  
  700.     ;  Determine the window value of plot window, wDraw1.
  701.     ;
  702.     WIDGET_CONTROL, wDraw1ID, GET_VALUE=cPwindow1
  703.  
  704.     ;  Determine the window value of plot window, wDraw2.
  705.     ;
  706.     WIDGET_CONTROL, wDraw2ID, GET_VALUE=cPwindow2
  707.  
  708.     ;  Set the active plot window to cPwindow2.
  709.     ;
  710.     WSET, cPwindow2
  711.  
  712.     ;  Get the character scaling factor.
  713.     ;
  714.     charscale = 8.0/!d.X_CH_SIZE
  715.  
  716.  
  717.     cFuture=FINDGEN(40)
  718.  
  719.     ;  Initialize the info structure.
  720.     ;
  721.     info={ $
  722.         cYdata:TSdataStr.cYdata, $          ; array, y coordinates
  723.         cXdata:TSdataStr.cXdata, $          ; array, x coordinates
  724.         cYmax:TSdataStr.cYmax, $            ; Max value of cYdata
  725.         cXmax:TSdataStr.cXmax, $            ; Max value of cXdata
  726.         cYmin:TSdataStr.cYmin, $            ; Min value of cYdata
  727.         cNy:TSdataStr.cNy, $                ; integer number of data in cYdata
  728.         cFcastmax:TSdataStr.cFcastmax, $    ; integer Max number of forecast
  729.         cOrder:cOrder, $                    ; Forecasting model Order parameter
  730.         cNfcast:cNfcast, $                  ; Number of forecast
  731.         cFuture:cFuture, $                  ; array, Future forecast
  732.         cPwindow1:cPwindow1, $              ; index to window 1
  733.         cPwindow2:cPwindow2 , $             ; index to window 2
  734.         wLabel1ID:wLabel2ID,$               ; Widget  label 2 ID
  735.         wLabel2ID:wLabel2ID,$               ; Widget  label 2 ID
  736.         WText: wText, $                     ; Widget text IDs for tips
  737.         SText: sText, $                     ; Text structure for tips
  738.         wDraw1ID:wDraw1ID,$                 ; Widget  Draw 1 ID
  739.         wDraw2ID:wDraw2ID,$                 ; Widget  Draw 2 ID
  740.         wOrderSlider:wOrderSlider,$         ; Order of parameters slider
  741.         wNumberSlider:wNumberSlider,$       ; Number of parameters slider
  742.         wAboutButton:wAboutButton , $       ; About button
  743.         wGenerateButton:wGenerateButton , $ ; Generate time series button
  744.         wQuitButton:wQuitButton , $         ; Quit button
  745.         colorTable:colorTable, $            ; Original Color Table
  746.         Charscale: charscale, $             ; Character scaling factor
  747.         plotFont:plotFont, $                ; Original Font
  748.         PreviousY : previousY, $            ; !Y system variable
  749.         PreviousX : previousX, $            ; !X system variable
  750.         groupBase: groupBase $              ; Base of Group Leader
  751.     }
  752.     
  753.     ;  Register the info structure in
  754.     ;  the user value of the top-level base.
  755.     ;
  756.     WIDGET_CONTROL, wTopBase, SET_UVALUE=info, /NO_COPY
  757.     
  758.     ;  Create a structure similar to an event such that the
  759.     ;  functions AUTOCORR and AUTOFCAST can be executed
  760.     ;  before the XMANAGER command has been called.
  761.     ;     
  762.     passwtop = {ID:0L, TOP:wTopBase, HANDLER:0L}
  763.  
  764.     ;  Compute the initial forecast.
  765.     ;
  766.     AutoFcast, passwtop
  767.      
  768.     ;  Compute the initial sample autocorrelation.
  769.     ;
  770.     AUTOCORR, passwtop
  771.  
  772.     ;  Destroy the starting up window.
  773.     ;
  774.     WIDGET_CONTROL, drawbase, /DESTROY
  775.  
  776.     ;  Map the top level base.
  777.     ;
  778.     WIDGET_CONTROL, wTopBase, MAP=1
  779.  
  780.  
  781.     ;  Register with XMANAGER.
  782.     ;
  783.     XMANAGER, "D_Forecast", wTopBase, $
  784.         EVENT_HANDLER="ForecastEventHdlr", CLEANUP="CleanUpForecast", $
  785.         /NO_BLOCK
  786.  
  787. end   ; of D_Forecast
  788.  
  789.